package com.kangyi.user.service.Impl;

import cn.hutool.core.collection.CollUtil;
import com.kangyi.constant.MessageConstant;
import com.kangyi.dto.AddNursingHomeBooking;
import com.kangyi.entity.NursingHome;
import com.kangyi.entity.NursingHomeBooking;
import com.kangyi.exception.DataNotExistException;
import com.kangyi.user.mapper.NursingHomeBookingMapper;
import com.kangyi.user.mapper.FeeMapper;
import com.kangyi.user.mapper.NursingHomeMapper;
import com.kangyi.user.service.NursingHomeBookingService;
import com.kangyi.utils.AuthUtil;
import com.kangyi.vo.FeeVO;
import com.kangyi.vo.NursingHomeBookingVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.kangyi.entity.NursingHomeBooking.PENDING_REVIEW;

/**
 * @author Blank
 * @version 1.0
 */
@Service
public class NursingHomeBookingServiceImpl implements NursingHomeBookingService {

    @Autowired
    private NursingHomeBookingMapper nursingHomeBookingMapper;

    @Autowired
    private NursingHomeMapper nursingHomeMapper;

    @Override
    public List<NursingHomeBookingVO> inquiryNursingHomeBooking(Integer userId) {

        List<NursingHomeBookingVO> nursingHomeBookingVOS = nursingHomeBookingMapper.inquiryNursingHomeBooking(userId);

        if (CollUtil.isEmpty(nursingHomeBookingVOS)) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }

        return nursingHomeBookingVOS;
    }

    @Override
    public void deleteNursingHomeBooking(Integer id) {

        NursingHomeBooking nursingHomeBooking = nursingHomeBookingMapper.selectById(id);

        if (nursingHomeBooking == null) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }

        AuthUtil.validateUserPermission(nursingHomeBooking.getUserId());

        nursingHomeBookingMapper.deleteNursingHomeBooking(id);
    }

    @Override
    public void addNursingHomeBooking(AddNursingHomeBooking addNursingHomeBooking) {

        if (addNursingHomeBooking == null) {
            throw new DataNotExistException(MessageConstant.DATA_LACK);
        }

        NursingHome nursingHome = nursingHomeMapper.selectById(addNursingHomeBooking.getNursingHomeId());

        if (nursingHome == null) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }

        NursingHomeBooking nursingHomeBooking = NursingHomeBooking.builder()
                .userId(addNursingHomeBooking.getUserId())
                .nursingHomeId(addNursingHomeBooking.getNursingHomeId())
                .name(addNursingHomeBooking.getName())
                .phone(addNursingHomeBooking.getPhone())
                .number(addNursingHomeBooking.getNumber())
                .notes(addNursingHomeBooking.getNotes())
                .bookingTime(addNursingHomeBooking.getBookingTime())
                .status(PENDING_REVIEW)
                .build();

        nursingHomeBookingMapper.addNursingHomeBooking(nursingHomeBooking);
    }
}
